package com.darkfire.weixinJDBC;

import org.json.JSONException;
import org.json.JSONObject;

public class WeiXinJDBC {
    private  String envId = "";
    private  String access_token = "";
    private  String app_secret = "";
    private  String appid="";
    private  String selectSQL = "https://api.weixin.qq.com/tcb/databasequery?access_token=";
    private  String updateSQL = "https://api.weixin.qq.com/tcb/databaseupdate?access_token=";
    private  String delSQL = "https://api.weixin.qq.com/tcb/databasedelete?access_token=";
    private  String insertSQL = "https://api.weixin.qq.com/tcb/databaseadd?access_token=";



    //初始化配置
    public WeiXinJDBC(String envId,String appid,String app_secret){
        this.envId = envId;
        this.app_secret = app_secret;
        this.appid = appid;
    }

    //执行查询语句
    public JSONObject execute_Select_SQL(String sql) {
        System.out.println("jdbc_select->"+sql);
        //执行SQL语句
        String param = "{   \"env\":\""+this.envId+"\",   \"query\": \""+sql+"\" }";
        String res = Tools.getInstance().sendPost(selectSQL,param);
        try{
            JSONObject obj = new JSONObject(res);
            System.out.println("obj->"+obj);
            if((Integer)obj.get("errcode") == 0 ){
                //表示获取成功
                return new JSONObject(res);
            }else{
                //表示token过期重新获取
                getAccess_token();
                res = Tools.getInstance().sendPost(selectSQL,param);
                return new JSONObject(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(res);
        return null;

    }

    //执行删除语句
    public JSONObject execute_Delete_SQL(String sql)  {
        //执行SQL语句
        System.out.println("jdbc_delete->"+sql);
        String param = "{   \"env\":\""+this.envId+"\",   \"query\": \""+sql+"\" }";
        String res = Tools.getInstance().sendPost(delSQL,param);
        System.out.println(res);
        try{
            JSONObject obj = new JSONObject(res);
            System.out.println("obj->"+obj);
            if((Integer)obj.get("errcode") == 0 ){
                //表示获取成功
                return new JSONObject(res);
            }else{
                //表示token过期重新获取
                getAccess_token();//重新获取token
                res = Tools.getInstance().sendPost(delSQL,param);
                return new JSONObject(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    //执行插入语句
    public JSONObject execute_Insert_SQL(String sql) throws JSONException {
        System.out.println("jdbc_insert->"+sql);
        //执行SQL语句
        String param = "{   \"env\":\""+this.envId+"\",   \"query\": \""+sql+"\" }";
        String res = Tools.getInstance().sendPost(insertSQL,param);
        System.out.println(res);
        try{
            JSONObject obj = new JSONObject(res);
            if((Integer)obj.get("errcode") == 0 ){
                //表示获取成功
                return new JSONObject(res);
            }else{
                //表示token过期重新获取
                getAccess_token();
                res = Tools.getInstance().sendPost(insertSQL,param);
                return new JSONObject(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    //执行更新语句
    public JSONObject execute_Update_SQL(String sql) throws JSONException {
        System.out.println("jdbc_update->"+sql);
        String param = "{   \"env\":\""+this.envId+"\",   \"query\": \""+sql+"\" }";
        String res = Tools.getInstance().sendPost(updateSQL,param);
        try{
            JSONObject obj = new JSONObject(res);
            System.out.println("obj->"+obj);
            if((Integer)obj.get("errcode") == 0 ){
                //表示获取成功
                return new JSONObject(res);
            }else{
                //表示token过期重新获取
                getAccess_token();
                res = Tools.getInstance().sendPost(updateSQL,param);
                return new JSONObject(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject().put("status","执行语句失败！");
    }

    //执行任意SQL语句
    public JSONObject execute_Any_SQL(String sql) throws JSONException {
        System.out.println("jdbc_any->"+sql);
        String param = sql;
        String res = Tools.getInstance().sendPost(updateSQL,param);
        try{
            JSONObject obj = new JSONObject(res);
            System.out.println("obj->"+obj);
            if((Integer)obj.get("errcode") == 0 ){
                //表示获取成功
                return new JSONObject(res);
            }else{
                //表示token过期重新获取
                getAccess_token();
                res = Tools.getInstance().sendPost(updateSQL,param);
                return new JSONObject(res);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JSONObject().put("status","执行语句失败！");
    }


    //获取微信云数据库执行token
    public void getAccess_token(){
        if (envId==null||app_secret==null||appid==null){
            return;
        }
        String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+app_secret;
        String res = Tools.getInstance().sendGet(token_url);
        try{
            JSONObject obj = new JSONObject(res);
            access_token = (String) obj.get("access_token");
            this.selectSQL = this.selectSQL + this.access_token;
            this.updateSQL = this.updateSQL + this.access_token;
            this.delSQL = this.delSQL + this.access_token;
            this.insertSQL = this.insertSQL + this.access_token;
            System.out.println("jdbcToken->"+access_token);
        }catch (Exception e){
            System.out.println("创建token失败,请检查环境id、小程序id密钥是否正确！");
        }

    }

    public String getEnvId() {
        return envId;
    }

    public String getApp_secret() {
        return app_secret;
    }

    public String getAppid() {
        return appid;
    }

    public String getSelectSQL() {
        return selectSQL;
    }

    public String getUpdateSQL() {
        return updateSQL;
    }

    public String getDelSQL() {
        return delSQL;
    }

    public String getInsertSQL() {
        return insertSQL;
    }





}
